package com.erp.module.faHuoDanJu.faHuo2

import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.erp.R
import com.erp.bean.CarBean
import com.erp.module.faHuoDanJu.FaHuoDanJuContract
import com.erp.myAdapter.CarKAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_danju.*

/**
 * Created by Administrator on 2018/11/9/009.
 * 作者：刘洋
 */
class FragmentFaHuo2 : BaseKFragment(), View.OnClickListener , AdapterView.OnItemClickListener, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, FaHuoDanJuContract.FaHuoKView {

    var dialog: AlertDialog? = null
    lateinit var carlist: MutableList<CarBean>
    lateinit var adapter : CarKAdapter
    lateinit var P : IFaHuoP2
    override fun initData() {
        carlist = ArrayList()
        P = IFaHuoP2(context!!, this, carlist)
        BuOnum.setOnClickListener(this)
        BuCarNunble.setOnClickListener(this)
        adapter= CarKAdapter(context!!, carlist)
        P.gainData("FaHuo2")
        LvCarNumble.adapter=adapter
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        LvCarNumble.onItemClickListener = this
        Pull.headerRefreshing()
    }
    private val f = "FragmentFahuo"

    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_danju
    }
    override fun onClick(v: View?) {
        when(v){
            BuOnum -> {
                dialog = Myutil.showupdatedialog(activity, "获取车次号信息", false)
                P.getCarNumble(EtSouSuo.text.toString(),false)
            }
            BuCarNunble -> {
                dialog = Myutil.showupdatedialog(activity, "获取车次号信息", false)
                P.getCarNumble(EtSouSuo.text.toString(),true)
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when(parent){
            LvCarNumble -> {
                val intent = Intent(activity, FaHuoCarAvtivity::class.java)
                intent.putExtra("CarNumBle",carlist[position].CarNumber)
                startActivity(intent)
            }
        }
    }
    override fun onHeaderRefresh(view: PullToRefreshView?) {
        Log.d("getCarShuJu","onHeaderRefresh")
        P.xiala()
    }

    override fun onFooterRefresh(view: PullToRefreshView?) {
        P.shangla()
    }
    override fun xialaOk() {
        dialog?.dismiss()
        adapter.notifyDataSetChanged()
        Pull?.onHeaderRefreshComplete()
    }

    override fun xialaNo() {
        dialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        adapter.notifyDataSetChanged()
        Pull.onFooterRefreshComplete()
    }

    override fun shanglaNo() {
        Pull.onFooterRefreshComplete()
    }

    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }
}